import {
  oilPage
} from '../services/car';
import React from 'react';
import {DatePicker, Input,} from 'antd';
const { RangePicker } = DatePicker;
/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '油卡编号',
    dataIndex: 'oilNo',
    width: '150px',
  },
  {
    title: '车牌号',
    dataIndex: 'carNo',
    width: '80px',
  },
  {
    title: '司机',
    dataIndex: 'driver',
    width: '80px',
  },
  {
    title: '上次公里数',
    dataIndex: 'kilometresStart',
    width: '80px',
  },
  {
    title: '本次公里数',
    dataIndex: 'kilometresEnd',
    width: '80px',
  },{
    title: '加油升数',
    dataIndex: 'oilCount',
    width: '60px',
  },
  {
    title: '油价',
    dataIndex: 'oilPrice',
    width: '60px',
  },
  {
    title: '金额',
    dataIndex: 'oilAmount',
    width: '60px',
  },
  {
    title: '实际金额',
    dataIndex: 'oilOffAmount',
    width: '60px',
  },
  {
    title: '百公里油耗',
    dataIndex: 'kwh',
    width: '60px',
  },
  {
    title: '加油时间',
    dataIndex: 'oilTime',
    width: '120px',
  },
  {
    title: '加油支付单号',
    dataIndex: 'oilPayNo',
    width: '120px',
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '油卡号',
        name: 'oilNo',
        component: <Input placeholder="请输入油卡号" />,
      },
      {
        md: 6,
        label: '车牌号',
        name: 'carNo',
        component: <Input placeholder="请输入车牌号" />,
      },
      {
        md: 6,
        label: '司机',
        name: 'driver',
        component: <Input placeholder="请输入司机" />,
      },
      {
        md: 6,
        label: '加油时间',
        name: 'createTimeArray',
        component: <RangePicker format="YYYY-MM-DD HH:mm:ss" />,
      },
    ],
  },
];



export default {
  namespace: 'carOil',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectedRows: [], // 表格选择行
    oilList: [], // 油卡下拉选择
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(oilPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(insertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
  },

  reducers: {

    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },

    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload.selectedRowKeys,
        selectedRows: payload.selectedRows
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: [], // 表格选择ID
          selectedRows: [], // 表格选择行
        };
      } else {
        return {
          ...state,
        };
      }
    },
  },
};
